package com.example.lenovo.wechattest;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView mBtnWeChat;
    private ImageView mBtnContact;
    private ImageView mBtnFind;
    private ImageView mBtnMe;

    private TextView mTextWeChat;
    private TextView mTextContact;
    private TextView mTextFind;
    private TextView mTextMe;

    private WeChatFragment mFragmentWeChat;
    private ContactFragment mFragmentContact;
    private FindFragment mFragmentFind;
    private MeFragment mFragmentMe;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mBtnWeChat = findViewById(R.id.btn_wechat);
        mBtnContact = findViewById(R.id.btn_contact);
        mBtnFind = findViewById(R.id.btn_find);
        mBtnMe = findViewById(R.id.btn_me);

        mTextWeChat = findViewById(R.id.text_wechat);
        mTextContact = findViewById(R.id.text_contact);
        mTextFind = findViewById(R.id.text_find);
        mTextMe = findViewById(R.id.text_me);

        mFragmentWeChat = null;
        mFragmentContact = null;
        mFragmentFind = null;
        mFragmentMe = null;

        mBtnWeChat.setOnClickListener(this);
        mBtnContact.setOnClickListener(this);
        mBtnFind.setOnClickListener(this);
        mBtnMe.setOnClickListener(this);
        mTextWeChat.setOnClickListener(this);
        mTextContact.setOnClickListener(this);
        mTextFind.setOnClickListener(this);
        mTextMe.setOnClickListener(this);

        selectTab(1);
    }

    @Override
    public void onClick(View v) {
        setTabDefaultColor();
        switch (v.getId()) {
            case R.id.btn_wechat:
                mBtnWeChat.setImageResource(R.mipmap.weixin_pressed);
                mTextWeChat.setTextColor(0xFF45C01A);
                selectTab(1);
                break;
            case R.id.btn_contact:
                mBtnContact.setImageResource(R.mipmap.contact_list_pressed);
                mTextContact.setTextColor(0xFF45C01A);
                selectTab(2);
                break;
            case R.id.btn_find:
                mBtnFind.setImageResource(R.mipmap.find_pressed);
                mTextFind.setTextColor(0xFF45C01A);
                selectTab(3);
                break;
            case R.id.btn_me:
                mBtnMe.setImageResource(R.mipmap.profile_pressed);
                mTextMe.setTextColor(0xFF45C01A);
                selectTab(4);
                break;
        }
    }

    public void selectTab(int index) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        if (mFragmentWeChat != null) {
            transaction.hide(mFragmentWeChat);
        }
        if (mFragmentContact != null) {
            transaction.hide(mFragmentContact);
        }
        if (mFragmentFind != null) {
            transaction.hide(mFragmentFind);
        }
        if (mFragmentMe != null) {
            transaction.hide(mFragmentMe);
        }

        switch (index) {
            case 1:
                if (mFragmentWeChat == null) {
                    mFragmentWeChat = new WeChatFragment();
                    transaction.add(R.id.fragment_test, mFragmentWeChat);
                }else {
                    transaction.show(mFragmentWeChat);
                }
                break;
            case 2:
                if (mFragmentContact == null) {
                    mFragmentContact = new ContactFragment();
                    transaction.add(R.id.fragment_test, mFragmentContact);
                }else {
                    transaction.show(mFragmentContact);
                }
                break;
            case 3:
                if (mFragmentFind == null) {
                    mFragmentFind = new FindFragment();
                    transaction.add(R.id.fragment_test, mFragmentFind);
                }else {
                    transaction.show(mFragmentFind);
                }
                break;
            case 4:
                if (mFragmentMe == null) {
                    mFragmentMe = new MeFragment();
                    transaction.add(R.id.fragment_test, mFragmentMe);
                }else {
                    transaction.show(mFragmentMe);
                }
                break;
        }
        transaction.commit();
    }

    private void setTabDefaultColor() {
        mBtnWeChat.setImageResource(R.mipmap.weixin_normal);
        mBtnContact.setImageResource(R.mipmap.contact_list_normal);
        mBtnFind.setImageResource(R.mipmap.find_normal);
        mBtnMe.setImageResource(R.mipmap.profile_normal);
        mTextWeChat.setTextColor(0xFFFFFFFF);
        mTextContact.setTextColor(0xFFFFFFFF);
        mTextFind.setTextColor(0xFFFFFFFF);
        mTextMe.setTextColor(0xFFFFFFFF);
    }
}
